package com.moguo.managementsystembackend.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moguo.managementsystembackend.model.User;
import com.moguo.managementsystembackend.model.request.UserRegisterRequest;
import com.moguo.managementsystembackend.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.moguo.managementsystembackend.contant.UserConstant.ADMIN_ROLE;
import static com.moguo.managementsystembackend.contant.UserConstant.USER_LOGIN_STATE;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("register")
    public Long userRegister(@RequestBody UserRegisterRequest  userRegisterRequest) {
      if(userRegisterRequest  == null){
          return null;
      }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if(StringUtils.isAllBlank(userAccount,userPassword,checkPassword)){
            return null;
        }
      return   userService.userRegister(userAccount,userPassword,checkPassword);
    }


    @PostMapping("login")
    public User userLogin(@RequestBody UserRegisterRequest  userRegisterRequest, HttpServletRequest request) {
        if(userRegisterRequest  == null){
            return null;
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        if(StringUtils.isAllBlank(userAccount,userPassword)){
            return null;
        }
        return   userService.userLogin(userAccount,userPassword,request);
    }

    @GetMapping("/search")
    public List<User> searchUsers(String username,HttpServletRequest request){
       if(!isAdmin(request)){
           return new ArrayList<>();
       }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isAllBlank(username)){
            queryWrapper.like("username",username);
        }
       List<User> userList=userService.list(queryWrapper);
        return userList.stream().map(user -> {
            user.setUserPassword(null);
            return userService.getSafetyUser(user);
        }).collect(Collectors.toList());
    }

    @PostMapping("/delete")
   public boolean deleteUser(@RequestBody Long id,HttpServletRequest request){
        if(!isAdmin(request)){
            return false;
        }
        if(id<=0){
            return false;
        }
        return userService.removeById(id);

    }

    /**
    * @Description: 是否为管理员
    * @Param: [request]
    * @return: boolean
    * @Author: moguo
    * @Date: 2024/5/4
    */
    private boolean isAdmin(HttpServletRequest request){
        //1、鉴权，仅管理员可查询
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
        User user = (User) userObj;
        return  user!=null && user.getUserRole()!=ADMIN_ROLE;

        }
    }



